package test;

import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.serial.SerialAddress;
import org.apache.mina.transport.serial.SerialConnector;
import org.apache.mina.transport.serial.SerialAddress.DataBits;
import org.apache.mina.transport.serial.SerialAddress.FlowControl;
import org.apache.mina.transport.serial.SerialAddress.Parity;
import org.apache.mina.transport.serial.SerialAddress.StopBits;

import jtarget.support.Configuration;
import jtarget.targetagent.TargetMessageMode2Parser;

public class QueryBoard extends JFrame implements ActionListener {

    public static void main(String[] args) throws InterruptedException {
        QueryBoard w = new QueryBoard();
        w.ConnectToSerial();
    }

    public QueryBoard() {
        this.setTitle("查询靶机状态");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600, 70);
        GridLayout gridLayout = new GridLayout(0, 4);
        Panel p = new Panel();
        p.setLayout(gridLayout);
        p.add(label);
        p.add(txtNo);
        p.add(btn1);
        btn1.setActionCommand("query");
        btn1.addActionListener(this);
        getContentPane().add(p);
        setVisible(true);
    }

    public void ConnectToSerial() throws InterruptedException {
        Configuration conf = Configuration.getInstance();
        conf.initialize("conf/server.properties");
        IoConnector connector = new SerialConnector();
        CommandHandler handler = new CommandHandler();
        connector.setHandler(handler);
        SerialAddress portAddress = new SerialAddress(conf.getSerialName(), conf.getSerailBauds(),
                DataBits.valueOf(conf.getSerialDatabits()), StopBits.valueOf(conf.getSerialStopBits()),
                Parity.valueOf(conf.getSerialParity()), FlowControl.valueOf(conf.getSerialFlowControl()));
        ConnectFuture future = connector.connect(portAddress);
        future.await();
        session = future.getSession();
        session.getConfig().setUseReadOperation(true);
        messageParser = new TargetMessageMode2Parser();
    }

    private TargetMessageMode2Parser messageParser;
    private IoSession session;
    private JLabel label = new JLabel("查询靶机状态");
    private JTextField txtNo = new JTextField();
    private JButton btn1 = new JButton("查询确认");

    @Override
    public void actionPerformed(ActionEvent e) {
        if (txtNo.getText() == null || txtNo.getText().length() == 0) {
            JOptionPane.showMessageDialog(null, "Enter the modified target number", "error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        short addr = Short.parseShort(txtNo.getText());
        switch (e.getActionCommand()) {
            case "query":
                session.write(messageParser.RequestBulletMessage((byte) addr));
        }
    }

}
